<?php
namespace app\index\model;
use think\Model;
use think\Request;
use think\Session;
class Admin extends Model
{
	//利用管理员id查询权限
    //$id管理员id
    //$name 规则标识
    public function isadmin($id=0,$name){
        //利用管理员id查询规则组id
        $groupid=model('authgroupaccess')
            ->where('adminid',$id)
            ->field('groupid')
            ->select();
        //把规则组id储存成字符串以“，”链接
        $group="0";
        foreach ($groupid as $k){
            $group.=','.$k['groupid'];
        }
        //把规则组id处理成数组
        $groupids=explode(",",$group);
        //利用规则组id查询规则id
        $ruleid=model('authgroup')->where(['status'=>['=','1'],'id'=>['in',$groupids]])->select();
        //把规则id储存成字符串以“，”链接
        $rules="0";
        foreach ($ruleid as $k){
            $rules.=','.$k['rules'];
        }
        //把规则id处理成数组
        $ruleids=explode(",",$rules);
        //利用规则id和$name查出有无此权限
        $bool=model('authrule')->where(['status'=>['=','1'],'id'=>['in',$ruleids],'name'=>['=',$name]])->find();
        return $bool;
    }
    //查询所有业务员
    public function secondadmin(){
        $res = model('admin')->where('isadmin','0')->select();
        return $res;
    }
    //查询业务员有多少权限组
    public function adminGroupAll(){
        $res=$this->alias('a')
            ->join('authgroupaccess b','b.adminid=a.id','left')
            ->field('a.id,account,groupid,isadmin')
            ->select();
        foreach($res as $k){
            $arr[]=$k->toArray();
        }
        //把管理组id处理成个数
        for ($i=0;$i<count($arr);$i++){
            //判断该管理员有无管理组数据
            if($arr[$i]['groupid'])
                $arr[$i]['groupid']=count(explode(',',$arr[$i]['groupid']));
            else
                $arr[$i]['groupid']=0;
        }
        return $arr;
    }
    //查询业务员权限组id
    public function ids($id){
        $res=$this->alias('a')
            ->join('authgroupaccess b','b.adminid=a.id','left')
            ->where('a.id',$id)
            ->field('groupid')
            ->find();
        $arr=explode(',',$res['groupid']);
        return $arr;
    }
    protected $resultSetType = 'collection';
    /**
     * 生成随机码,然后加入到密码中
     */
    public function randomcode(){
        $pattern = '1234567890aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUVvWwXxYyZz';
        for($i=0,$key='';$i<20;$i++)
        {
            $key .= $pattern{mt_rand(0,35)};
        }
        return $key;
    }
    //修改管理员信息
    public function edit($id,$account,$isadmin){
        $admin      = new Admin;
        $admin->save([
            'account'      => $account,
            'isadmin'      => $isadmin
        ],['id' => $id]);
        return true;
    }
    public function editmyself($id,$account){
        $admin      = new Admin;
        $admin->save([
            'account'  => $account
        ],['id' => $id]);
        return true;
        
    }
    public function editroot($id,$root){
        $admin      = new Admin;
        $randomcode =$admin->randomcode();
        $admin->save([
            'root'  => $root.$randomcode,
            'randomcode' => $randomcode
        ],['id' => $id]);
        return true;
        
    }
    //修改输出时的权限名字
    public function getIsadminAttr($value)
    {
        $status = [1=>'超级管理员',0=>'业务员'];
        return $status[$value];
    }
    //每个业务员管理的店铺数量查询
    public function businessAll(){
        $res=$this->alias('a')
            ->join("agency","agency.adminid=a.id",'left')
            ->where("isadmin","0")
            ->group("a.id")
            ->field('account,count(adminid) as count,a.id')
            ->select();
        return $res;
    }
    //查询一条信息
    public function OneData($id){
        $res=$this->where("id",$id)->find();
        return $res;
    }
}